package com.wwj.feedback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wwj.common.constant.CommonCodeEnum;
import com.wwj.common.constant.CommonConstant;
import com.wwj.common.entitiy.R;
import com.wwj.feedback.constant.ConstantFinal;
import com.wwj.feedback.entitiy.AnnouncementEntity;
import com.wwj.feedback.service.AnnouncementService;
import com.wwj.feedback.to.AnnouncementTo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/announcement")
public class AnnouncementController {

    @Resource
    private AnnouncementService announcementService;

    /**
     * 查询最近三条公告
     * OpenFeign
     */
    @RequestMapping("/getAnnouncementTop3")
    private List<AnnouncementTo> getAnnouncementTop3() {
        return announcementService.getAnnouncementTop3();
    }

    /**
     * 发布公告
     *  http://127.0.0.1:88/api/announcement/submit
     */
    @PostMapping("/submit")
    private R<Object> submit(@RequestBody AnnouncementEntity announcementEntity){
        // 保存数据
        boolean flag = announcementService.save(announcementEntity);
        if (flag) {
            return R.ok("公告发布成功");
        }else {
            return R.error(CommonCodeEnum.ERROR.getCode(), CommonCodeEnum.ERROR.getMsg());
        }
    }

    /**
     * 获取最近的三条公告
     * http://127.0.0.1:88/api/announcement/getAnnouncement
     */
    @GetMapping("/getAnnouncement")
    private R<Object> getAnnouncement(){
        QueryWrapper<AnnouncementEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.le(ConstantFinal.FIELD_ANNOUNCE_TIME, new Date());
        queryWrapper.orderByDesc(ConstantFinal.FIELD_ANNOUNCE_TIME).last("limit 3");
        List<AnnouncementEntity> list = announcementService.list(queryWrapper);
        return R.ok(CommonCodeEnum.SUCCESS.getCode(),CommonCodeEnum.SUCCESS.getMsg(),list);
    }

}
